package tt.dz.biz.dao.smsConfigure;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Repository;
import tt.dz.biz.dao.BaseDAO;
import tt.dz.common.LogUtil;
import tt.dz.vo.SmsConfigureVO;
import tt.dz.vo.SmsParameterShowVO;
import tt.dz.vo.SmsParameterVO;
import tt.dz.vo.SmsSendRecord;

@Repository("smsConfigureDAO")
public class SmsConfigureDAOImpl
  extends BaseDAO
{
  public static final String INSERT_SMS_CONFIGURE = "insert into ms_sms_configure(id,merchant_code,sms_channel,url,comment,is_used) values(:id,:merchantCode,:smsChannel,:url,:comment,:isUsed)";
  public static final String UPDATE_SMS_CONFIGURE = "update ms_sms_configure set merchant_code=:merchantCode,sms_channel=:smsChannel,url=:url,comment=:comment,is_used=:isUsed where id=:id";
  public static final String INSERT_SMS_PARAMETER = "insert into ms_sms_parameter(sms_config_id,param_name,param_key,param_value) values(:smsConfigId,:paramName,:paramKey,:paramValue)";
  public static final String SMS_CONFIGURE_VO_LIST = "select id,merchant_code,sms_channel,url,comment,is_used from ms_sms_configure ";
  public static final String SMS_PARAMETER_VO_LIST = "select sms_config_id,param_name,param_key,param_value from ms_sms_parameter where sms_config_id=:id  ";
  public static final String DELETE_SMS_CONFIGURE = "delete from ms_sms_configure where id=:id";
  public static final String DELETE_SMS_PARAMETER = "delete from ms_sms_parameter where sms_config_id=:id";
  public static final String GET_CONFIGS_BY_MERCHANTCODE = "select id,merchant_code,sms_channel,url,comment,is_used from ms_sms_configure where merchant_code=:merchantCode and is_used=1";
  public static final String SAVE_SMS_SEND_RECORD = "insert into  ms_sms_send_record(merchant_code,mobile_no,sms_channel,business_type,message_content,content_length,send_message_time) values(:merchantCode,:mobileNo,:smsChannel,:businessType,:messageContent,:contentLength,:sendMessageTime)";
  public static final String LIST_ALL_SMSRECORD = "select merchant_code,mobile_no,sms_channel,business_type,message_content,content_length,send_message_time  from ms_sms_send_record a where 1=1";
  public static final String LIST_ALL_SMSRECORD_COUNT = "select count( a.merchant_code)  from ms_sms_send_record a where 1=1";
  
  public int insertSmsConfigure(SmsConfigureVO smsConfigureVO)
  {
    LogUtil.debug("插入短息平台配置相关数据");
    return save("insert into ms_sms_configure(id,merchant_code,sms_channel,url,comment,is_used) values(:id,:merchantCode,:smsChannel,:url,:comment,:isUsed)", smsConfigureVO);
  }
  
  public int updateSmsConfigure(SmsConfigureVO smsConfigureVO)
  {
    LogUtil.debug("更新短息平台配置相关数据");
    return save("update ms_sms_configure set merchant_code=:merchantCode,sms_channel=:smsChannel,url=:url,comment=:comment,is_used=:isUsed where id=:id", smsConfigureVO);
  }
  
  public int insertSmsParameter(SmsParameterVO smsParameterVO)
  {
    LogUtil.debug("插入短息平台参数相关数据");
    return save("insert into ms_sms_parameter(sms_config_id,param_name,param_key,param_value) values(:smsConfigId,:paramName,:paramKey,:paramValue)", smsParameterVO);
  }
  
  public List<SmsConfigureVO> getSmsConfigureVOlist()
  {
    Map<String, Object> paramMap = new HashMap();
    MapSqlParameterSource parameters = new MapSqlParameterSource(paramMap);
    List<SmsConfigureVO> smsConfigureVOlist = queryForList("select id,merchant_code,sms_channel,url,comment,is_used from ms_sms_configure ", parameters, SmsConfigureVO.class);
    
    return smsConfigureVOlist;
  }
  
  public List<SmsParameterShowVO> getSmsParameterVOList(Map<String, Object> paramMap)
  {
    MapSqlParameterSource parameters = new MapSqlParameterSource(paramMap);
    List<SmsParameterShowVO> smsParameterVOList = queryForList("select sms_config_id,param_name,param_key,param_value from ms_sms_parameter where sms_config_id=:id  ", parameters, SmsParameterShowVO.class);
    
    return smsParameterVOList;
  }
  
  public int deleteSmsConfigure(Map<String, Object> paramMap)
  {
    MapSqlParameterSource parameters = new MapSqlParameterSource(paramMap);
    return save("delete from ms_sms_configure where id=:id", parameters);
  }
  
  public int deleteSmsParameter(Map<String, Object> paramMap)
  {
    MapSqlParameterSource parameters = new MapSqlParameterSource(paramMap);
    return save("delete from ms_sms_parameter where sms_config_id=:id", parameters);
  }
  
  public List<SmsConfigureVO> getConfigsByMerchantCode(String merchantCode)
  {
    Map<String, Object> paramMap = new HashMap();
    paramMap.put("merchantCode", merchantCode);
    MapSqlParameterSource parameters = new MapSqlParameterSource(paramMap);
    List<SmsConfigureVO> smsConfigureVOList = queryForList("select id,merchant_code,sms_channel,url,comment,is_used from ms_sms_configure where merchant_code=:merchantCode and is_used=1", parameters, SmsConfigureVO.class);
    
    return smsConfigureVOList;
  }
  
  public Map<String, String> convertParamMap(SmsConfigureVO smsConfig)
  {
    List<SmsParameterVO> smsParameterVOList = new ArrayList();
    Map<String, Object> paramMap = new HashMap();
    paramMap.put("id", smsConfig.getId());
    MapSqlParameterSource parameters = new MapSqlParameterSource(paramMap);
    smsParameterVOList = queryForList("select sms_config_id,param_name,param_key,param_value from ms_sms_parameter where sms_config_id=:id  ", parameters, SmsParameterVO.class);
    Map<String, String> map = new HashMap();
    for (SmsParameterVO smsParameterVO : smsParameterVOList) {
      map.put(smsParameterVO.getParamKey().trim(), smsParameterVO.getParamValue().trim());
    }
    return map;
  }
  
  public void saveSmsSendRecord(SmsSendRecord smsSendRecord)
  {
    LogUtil.debug("保存发送短信相关数据");
    save("insert into  ms_sms_send_record(merchant_code,mobile_no,sms_channel,business_type,message_content,content_length,send_message_time) values(:merchantCode,:mobileNo,:smsChannel,:businessType,:messageContent,:contentLength,:sendMessageTime)", smsSendRecord);
  }
}
